﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Configuration;

namespace Utils.DC.Web.UI
{
    public class MailWorker
    {
        public static void SendEmail(String receivers, String title, String body)
        {
            MailMessage objMail = new MailMessage(ConfigurationManager.AppSettings["Email"], receivers, title, body);
            System.Net.Mail.SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";//Ví dụ xử dụng SMTP của gmail                    
            smtp.Send(objMail);
        }

        public static void SendGmail(string mailTo, string commaDelimCCs, string subject, string message, bool isBodyHtml)
        {
            try
            {
                string userName = ConfigurationManager.AppSettings["MailFrom"];
                string password = ConfigurationManager.AppSettings["Password"];

                System.Net.Mail.MailMessage msg = new
                System.Net.Mail.MailMessage(userName, mailTo, subject, message);
                msg.IsBodyHtml = isBodyHtml;
                if (commaDelimCCs != "")
                    msg.CC.Add(commaDelimCCs);
                System.Net.NetworkCredential cred = new
                System.Net.NetworkCredential(userName, password);
                System.Net.Mail.SmtpClient mailClient = new
                System.Net.Mail.SmtpClient(ConfigurationManager.AppSettings["SMTP"], 587);
                mailClient.EnableSsl = true;
                mailClient.UseDefaultCredentials = false;
                mailClient.Credentials = cred;
                mailClient.Send(msg);
            }
            catch (Exception ex)
            {
                Common.FileWriter(ex.ToString());
            }
        }
    }
}
